const app = getApp()

Page({
  data: {
    cate_sid:0,
    tab:0,
    like:'star-o',
    like_color:'#888888',
    onLike:false,
    showShar:false
  },

  param: {
    store_id:0,
  },

	//生命周期函数--监听页面加载
	onLoad: function (options) {
    app.getParam(options,param =>{
      this.param.store_id = param.store_id;
      app.isLbs((rel)=>{
        this.setData({
          lat:app.globalData.lbs.lat,
          lng:app.globalData.lbs.lng
        })
      })
      this.getStore();
    })
	},
  //首页附近好店
  getStore: function () {
		app.api().Get('api/v1/ais/store/read', {store_id:this.param.store_id},(result) => {
			if (result.code == 200) {
				this.setData({
					store: result.data,
        })
        wx.setNavigationBarTitle({
          title: result.data.name,
        })
        this.getCoupon();
			}
		})
  },
  //获取优惠券
  getCoupon: function() {
    app.api().Get('api/v1/ais/coupon/store',{store_id:this.param.store_id,page:0},(result)=>{
      if (result.code == 200) {
        this.setData({
          coupon: result.data,
        });
      }
    })
  },
  //获取商家产品
  getShop: function() {
    app.api().Get('api/v1/ais/shop/store',{store_id:this.param.store_id,page:0},(result)=>{
      if (result.code == 200) {
        this.setData({
          shop: result.data,
        });
      }
    })
  },
  //点击Tab 
  onClickTab:function(e){
    var tab = parseInt(e.currentTarget.dataset.tab);
    this.setData({tab:tab})
    switch (tab) {
      case 1:
        this.getShop();
        break;
      case 2:
        this.getShop();
        break;
      case 3:
        this.getShop();
        break;
      default:
        this.getCoupon();
        break;
    }
  },
  //地图定位
  maps: function (e) {
    wx.openLocation({
      name: e.currentTarget.dataset.name,
      longitude: parseFloat(e.currentTarget.dataset.lng),
      latitude: parseFloat(e.currentTarget.dataset.lat),
      address: e.currentTarget.dataset.address
    });
  },
  //拨打电话
  callPhone: function (e) {
    app.callPhone(e);
  },
  //点赞
  onLike:function(){
    if ("star-o" == this.data.like){
      app.api().Post("api/v1/ais/store/like",{store_id:this.param.store_id},(rel)=>{
        this.setData({
          like:'star',
          like_color:'#FFA000',
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //分享
  onHideShare() {
    this.setData({
      showShare: !this.data.showShare
    });
  },
  //分享按钮
  onShareAppMessage: function () {
    this.setData({
      showShare:false
    });
    return {
      title: this.data.store.name,
      imageUrl: this.data.store.img,
      desc: this.data.store.address,
      path: '/packageIndex/pages/store/read?store_id='+this.param.store_id+'&ucode='+app.globalData.loginuser.ucode,
    }
  },
  onShareTimeline:function(item) {
    return {
      title: this.data.store.name,
      imageUrl: this.data.store.img,
      query:{
        store_id:this.param.store_id,
        ucode:app.globalData.loginuser.ucode,
      },
      path: '/packageIndex/pages/store/read',
    } 
  },
  onPaid:function (e) {
    wx.navigateTo({
      url: '/packagePay/index?store_id='+this.param.store_id,
    })
  },
})